<script setup>
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";
import {Plus} from "@element-plus/icons-vue";
import axios from "axios";

const dialogVisible =ref(false)
const formData=ref({})
const handleClose =()=>{
  dialogVisible.value = false;
  formData.value={}
  imageUrl.value=''
  if (uploadRef.value){
    uploadRef.value.clearFiles()
  }
  emits("dialogVisible",dialogVisible.value)
}
const props=defineProps({
  visible:{
    type:Boolean,
    required:false,
    default:false
  },
  advertisement:{
    type:Object,
    required:false,
    default:()=>{}
  }
})
watch(()=>props.visible,(newVal)=>{
  dialogVisible.value=newVal;
})
watch(()=>props.advertisement,(newVal)=>{
  formData.value=newVal;
})
const emits=defineEmits("")
const uploadRef=ref(null)
const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' && rawFile.type!=='image/webp') {
    ElMessage.error('您上传的头像必须是jpg或者png格式')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('您上传的图片大小必须2M以内')
    return false
  }
  return true
}
const imageUrl=ref('')
const handleAvatarSuccess =(resp,uploadFile)=>{
  imageUrl.value=resp;
  formData.value.imageUrl=resp;
  ElMessage.success("上传成功")
}
const submitForm=()=>{
  axios.post("/advertisement/saveOrUpdateAdvertisement",formData.value).then(resp=>{
    ElMessage.success(resp.data);
    handleClose();
  })
}
</script>

<template>
  <el-dialog
      v-model="dialogVisible"
      title="添加广告"
      width="500px"
      :close-on-click-modal="false"
      :before-close="handleClose"
  >
    <!-- 广告名称 -->
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="广告名称：" prop="adName">
        <el-input v-model="formData.adName" placeholder="请输入广告名称" />
      </el-form-item>

      <!-- 广告位置 -->
      <el-form-item label="广告位置：">
        <el-select v-model="formData.position" placeholder="请选择">
          <el-option label="首页轮播" value="首页轮播" />
          <el-option label="首页底部" value="首页底部" />
          <el-option label="首页右侧" value="首页右侧" />
          <el-option label="分类页顶部" value="分类页顶部" />
          <el-option label="个人中心" value="个人中心" />
          <el-option label="侧边栏" value="侧边栏" />
        </el-select>
      </el-form-item>

      <!-- 图片上传 -->
      <el-form-item label="图片" prop="imageUrl">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/api/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>

      <!-- 跳转地址 -->
      <el-form-item label="跳转地址（选填）：">
        <el-input v-model="formData.redirectUrl" placeholder="请输入链接（如：http://）" />
      </el-form-item>

      <!-- 发布时间 -->
      <el-form-item label="广告时间：">
        <el-date-picker
            v-model="formData.startTime"
            type="date"
            placeholder="请选择开始时间"
            :size="size"
        />
        <el-date-picker
            v-model="formData.endTime"
            type="date"
            placeholder="请选择结束时间"
            :size="size"
        />
      </el-form-item>
    </el-form>

    <!-- 底部按钮 -->
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="submitForm">保存</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>